﻿#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <Winsock2.h>
#include <Ws2tcpip.h>
#include <thread>
#include <chrono>
#include <string>


#pragma comment(lib, "Ws2_32.lib")
std::string ipaddr;
int reportstatus()
{
    // 初始化Winsock
    WSADATA wsaData;
    int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (result != 0) {
        std::cerr << "WSAStartup failed: " << result << std::endl;
        return 1;
    }

    // 创建客户端套接字
    SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (clientSocket == INVALID_SOCKET) {
        std::cerr << "Error creating socket: " << WSAGetLastError() << std::endl;
        WSACleanup();
        return 1;
    }

    // 连接到许可证服务器
    sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    serverAddr.sin_port = htons(12345);
    if (connect(clientSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
        std::cerr << "Error connecting to server: " << WSAGetLastError() << std::endl;
        closesocket(clientSocket);
        WSACleanup();
        return 1;
    }
    std::string message = ipaddr + "|" + "RUNNING";
    send(clientSocket, message.c_str(), message.length(), 0);
    closesocket(clientSocket);
}

int main() {
    // 初始化Winsock
    WSADATA wsaData;
    int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (result != 0) {
        std::cerr << "WSAStartup failed: " << result << std::endl;
        return 1;
    }

    // 创建客户端套接字
    SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (clientSocket == INVALID_SOCKET) {
        std::cerr << "Error creating socket: " << WSAGetLastError() << std::endl;
        WSACleanup();
        return 1;
    }

    // 连接到许可证服务器
    sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    std::cout << "请输入Ip:" << std::endl;
    std::cin >> ipaddr;
    serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    serverAddr.sin_port = htons(12345);
    if (connect(clientSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
        std::cerr << "Error connecting to server: " << WSAGetLastError() << std::endl;
        closesocket(clientSocket);
        WSACleanup();
        return 1;
    }

    // 向服务器发送序列号
    std::string licenseKey;
    std::cout << "请输入序列号" << std::endl;
    std::cin >> licenseKey;
    std::string message = ipaddr + "|" + licenseKey;
    send(clientSocket, message.c_str(), message.length(), 0);

    // 接收服务器响应
    char buffer[1024];
    int bytesReceived = recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
    if (bytesReceived > 0) {
        buffer[bytesReceived] = '\0';
        std::string response(buffer);
        if (response == "AUTHORIZED") {
            std::cout << "Access granted!" << std::endl;
            std::cout << "Program is running" << std::endl;
            while (true)
            {
                reportstatus();
                //休眠1分钟
                //std::this_thread::sleep_for(std::chrono::minutes(1));
                // 休眠5秒
                std::this_thread::sleep_for(std::chrono::seconds(5));
            }
        }
        else if (response == "OVERRUN")
        {
            std::cout << "Access denied! Try again later!" << std::endl;
        }
        else if (response == "REJECTED")
        {
            std::cout << "License error!" << std::endl;
        }
    }

    // 关闭客户端套接字
    closesocket(clientSocket);

    // 清理Winsock库
    WSACleanup();

    return 0;
}